﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace AutoTask
{
	public partial class Form1 : Form
	{
		#region 属性
		//设置信息的文件名称
		private readonly string _settingFileName = "Setting.json";
		//设置信息
		private SettingsInfo _settingsInfo;
		//主计时器
		private Timer _mainTimer;
		//提醒
		//private bool _isRemind = false;
		//已写周报
		private bool _isWriteWeekly = false;
		//已发周报
		private bool _isSendWeekly=false;
		#endregion

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			//设置
			_settingsInfo = Tools.Instance.GetJsonSetting<SettingsInfo>(_settingFileName);
			AutoRunToolStripMenuItem.Checked = _settingsInfo.IsAutoRun;
			
			//设置主计时器
			_mainTimer = new Timer();
			//十分钟执行一次 ---  600000
			_mainTimer.Interval = 60000;
			_mainTimer.Tick += _mainTimer_Tick;
			_mainTimer.Enabled = true;

			//监听通知气泡的事件
			notifyIcon1.BalloonTipClicked += NotifyIcon1_BalloonTipClicked;
			notifyIcon1.BalloonTipClosed += NotifyIcon1_BalloonTipClosed;

			//notifyIcon1.ShowBalloonTip(1, "提醒!快来看！", "今天还没写周报吧，快写周报!!!\n先写周报，再做事!!!", ToolTipIcon.Warning);
		}



		private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void NotifyDoubleClickEvent(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Normal;
		}

		//private void ShowInfo(object sender, EventArgs e)
		//{
		//	notifyIcon1.ShowBalloonTip(2, "xxxx", "---sfsdf--fs", ToolTipIcon.Info);
		//}

		private void toolStripComboBox1_Click(object sender, EventArgs e)
		{

		}

		
		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
		{

		}

		/// <summary>
		/// 开机启动变化
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void StartUpChanged(object sender, EventArgs e)
		{
			_settingsInfo.IsAutoRun = !_settingsInfo.IsAutoRun;
			AutoRunToolStripMenuItem.Checked = _settingsInfo.IsAutoRun;
			//设置开机启动
			Tools.Instance.SetAutoRun("AutoTask.exe", _settingsInfo.IsAutoRun);
			//保存设置
			Tools.Instance.SaveJsonSetting(_settingFileName, _settingsInfo);
		}


		#region 事件回调
		//主计时器
		private void _mainTimer_Tick(object sender, EventArgs e)
		{
			//更新实时的时间
			DateTime nowDateTime = DateTime.Now;

			DateTime fridayTime = GetFridayDay();

			string weeklyWord = $"Weekly\\{_settingsInfo.Name}-{fridayTime.ToString("yyyy.MM.dd")}.docx";
			//提交路径
			string submittedWeeklyWord= $"Weekly\\Submitted\\{_settingsInfo.Name}-{fridayTime.ToString("yyyy.MM.dd")}.docx";

			//5点过后 提醒写周报
			if (!_isWriteWeekly && nowDateTime.Hour >= 17)
			{
				//一个小时
				notifyIcon1.ShowBalloonTip(3600, "提醒!快来看！", "今天还没写周报吧，快写周报!!!\n先写周报，再做事!!!", ToolTipIcon.Warning);

				//DialogResult dialogResult= MessageBox.Show("今天还没写周报吧，快写周报!!!\n先写周报，再做事!!!","提醒！提醒！",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
				//if (dialogResult == DialogResult.OK)
				//{
				//	_isRemind = true;
				//	OpenWeekWord();
				//}
				//else if (dialogResult == DialogResult.Cancel)
				//{

				//}
			}

			//过了五点半 自动发送
			bool isReadSend = (nowDateTime.Hour >= 17 && nowDateTime.Minute > 30) || nowDateTime.Hour >= 18 ? true : false;

			//周五 5点半过后，提醒发送邮件
			if (nowDateTime.DayOfWeek==DayOfWeek.Friday
				&& isReadSend
				&& !WeeklyWord.Instance.WordSubmitted(submittedWeeklyWord))
			{
				notifyIcon1.ShowBalloonTip(60, "准备发送周报邮件了！", "这么久没有动作，我就自动发送了！！空白内容也发了！！\n"+ weeklyWord, ToolTipIcon.Warning);
				//自动发送邮件
				SendWeeklyMail();
			}

			Console.WriteLine(
				$"hour:{nowDateTime.Hour},week:{nowDateTime.DayOfWeek},year:{nowDateTime.Year},month:{nowDateTime.Month},Minute:{nowDateTime.Minute},Second:{nowDateTime.Second}");
		}

		//点击通知气泡的事件
		private void NotifyIcon1_BalloonTipClicked(object sender, EventArgs e)
		{
			//if (!_isRemind)
			//{
			//	OpenWeekWord();
			//	_isRemind = true;
			//}
		}

		//点击关闭气泡的事件
		private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
		{
			//throw new NotImplementedException();
		}

		//打开主窗口
		private void OpenMainWindow(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Maximized;
		}


		//点击发送周报的按钮
		private void OnSendWeeklyClick(object sender, EventArgs e)
		{
			//更新实时的时间
			DateTime nowDateTime = DateTime.Now;
			if (nowDateTime.DayOfWeek != DayOfWeek.Friday)
			{
				DialogResult dialogResult =
					MessageBox.Show("今天不是周五，确定发送周报么？", "Waring!!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
				if (dialogResult == DialogResult.OK)
				{
					SendWeeklyMail();
				}
				else if (dialogResult == DialogResult.Cancel)
				{
					return;
				}
			}
			else
				SendWeeklyMail();
			
		}

		//打开周报
		private void toolStripMenuItem2_Click(object sender, EventArgs e)
		{
			OpenWeekWord();
		}


		#endregion


		#region 内部函数

		//打开周报word文档
		private void OpenWeekWord()
		{
			DateTime fridayTime = GetFridayDay();

			string weeklyWord = $"Weekly\\{_settingsInfo.Name}-{fridayTime.ToString("yyyy.MM.dd")}.docx";

			//提交路径
			string submittedWeeklyWord = $"Weekly\\Submitted\\{_settingsInfo.Name}-{fridayTime.ToString("yyyy.MM.dd")}docx";
			if (WeeklyWord.Instance.WordSubmitted(submittedWeeklyWord))
				weeklyWord = submittedWeeklyWord;
			//打开周报
			WeeklyWord.Instance.OpenWord(weeklyWord);

			_isWriteWeekly = true;
		}

		//写周报邮件
		private void SendWeeklyMail()
		{
			if (_isSendWeekly)
				return;

			//更新实时的时间
			DateTime nowDateTime = DateTime.Now;

			DateTime fridayTime = GetFridayDay();

			string weeklyWord = $"Weekly\\{_settingsInfo.Name}-{fridayTime.ToString("yyyy.MM.dd")}.docx";
			//提交路径
			string submittedWeeklyWord = $"Weekly\\Submitted\\{_settingsInfo.Name}-{fridayTime.ToString("yyyy.MM.dd")}.docx";
			//自动发送邮件
			WriteWeeklyMail($"周报--{_settingsInfo.Name}--{fridayTime.ToString("yyyy.MM.dd")}", weeklyWord, submittedWeeklyWord);

			_isSendWeekly = true;
		}

		//发送周报
		private void WriteWeeklyMail(string title,string file,string dest)
		{
			string context = "\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t---auto task 自动发送!";
			if (!File.Exists(file))
			{
				context = "没有找到周报文件!!!" + context;
				file = "";
			}

			if (WeeklyEmail.Instance.SendWeeklyMail("coding2233@163.com", _settingsInfo.Name, _settingsInfo.ToEmail, "塞傲时代",
				title, context, file, "smtp.163.com", "coding2233@163.com", "coding2580"))
			{
				//成功发送 则将周报移动到已提交目录下
				WeeklyWord.Instance.MoveWord(file, dest);
			}
		}


		//获取周五的日子
		private DateTime GetFridayDay()
		{
			//更新实时的时间
			DateTime nowDateTime = DateTime.Now;
			for (int i = 0; i <= 7; i++)
			{
				DateTime temp=nowDateTime.AddDays(i);
				if (temp.DayOfWeek == DayOfWeek.Friday)
					return temp;
			}
			return nowDateTime;
		}


		#endregion


	}
}
